import { OSSService } from '@nest-public/nest-oss';
import { Controller, Post, UploadedFile, UseInterceptors, UseGuards } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiBody, ApiConsumes, ApiOperation, ApiProperty, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';

class FileUploadDto {
  @ApiProperty({ type: 'string', format: 'binary' })
  file: any;
}


@ApiTags('系统共用')
@Controller('app')
export class AppController {
  constructor(private readonly oSSService: OSSService) { }

  @UseGuards(AuthGuard('jwt-admin'))
  @ApiBearerAuth()
  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  @ApiConsumes('multipart/form-data')
  @ApiOperation({
    description: '接口说明',
    summary: '上传文件',
  })
  @ApiBody({
    description: 'List of cats',
    type: FileUploadDto,
  })
  async uploadFile(@UploadedFile('file') file) {
    console.log('hahah')

    const OSS_file = await this.oSSService.upload(file);
    return {
      name: OSS_file[0].path,
      url: OSS_file[0].src,
      message: OSS_file[0].message
    }
  }

  // async init() {
  //   let userAtt: User [] = await this.user.find()
  //   userAtt.forEach(async user => {
  //     if(user.init == false) {
  //       await this.user.update(user.id, { init: true })
  //       console.log(await this.user.findOne(user.id))
  //     }
  //   })
  // }
}
